<template>
	<view>
		<tree-list 
			v-if="tree.length>0"
			:checkList="checkList" 
			:props="prop" 
			:isCheck="true" 
			:trees="tree" 
			:relations="assets" 
			@sendValue="confirm" 
			@showRelations = "getAssets"/>
		<view class="btn box_sizing">
			<button class="sureBtn" type="primary" @click="backConfirm">确认</button>
		</view>
	</view>
</template>

<script>
	import TreeList from '@/components/business/asset-tree/tree.vue';
	import { queryDataByKey } from '@/api/platform/common.js'
	export default {
		components: {
			TreeList
		},
		data() {
			return {
				tree: [],
				prop: {
					label: 'name',
					children: 'children',
					multiple:false,
					nodes:true
				},
				checkList: [],
				list: [],
				assets: []
			}
		},
		onLoad() {
			this.list = this.getDataList("778907868320497664")
			this.getAssets()
		},
		onReady() {
			uni.showLoading({
				title:"加载中"
			})
			setTimeout(()=>{
				this.tree = this.list
				uni.hideLoading()
			},400)
		},
		methods: {
			getDataList(locaId) {
				const params = []
				params.push({
					key: "key",
					value: "sbwzst"
				})
				params.push({
					key: "Q^p_loca_id^SL",
					value: locaId
				})
				let list = []
				queryDataByKey(params).then(res=>{
					res.data.dataResult.forEach(item=>{
						let data = {
							id: item.loca_id,
							no: item.loca_no,
							name: item.loca_name
						}
						
						let children = []
						children = this.getDataList(item.loca_id)
						
						data.children = children
						list.push(data)
					})
				})
				return list
			},
			getAssets(item) {
				const params = []
				params.push({
					key: "key",
					value: "sbzcwzst"
				})
				if(item){
					params.push({
						key: "Q^loca_no^SL",
						value: item.no
					})
				}
				
				this.assets = []
				queryDataByKey(params).then(res=>{
					res.data.dataResult.forEach(item=>{
						this.assets.push({
							id: item.asset_id,
							no: item.asset_no,
							name: item.asset_name,
							checked: false,
							locaId: item.loca_id,
							locaNo: item.loca_no,
							locaName: item.loca_name
						})
					})
				})
			},
			//获取选中的
			confirm(val) {
				this.checkList = val
			},
			backConfirm() {
				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2]; //上一个页面
				prevPage.$vm.reciveAsset(this.checkList)
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss">
	.box_sizing {
		-webkit-box-sizing: border-box;
		-moz-box-sizing: border-box;
		box-sizing: border-box;
	}

	.btn {
		position: fixed;
		bottom: 0;
		padding: 16rpx;
		background-color: #fff;
		width: 100%;

		.sureBtn {
			background-color: #1E91E6;
			color: #fff;
		}
	}
</style>
